package com.zhkc.wisdom.common.swagger;

import com.zhkc.wisdom.common.configure.constant.ConfigureConstant;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * Description: Swagger配置
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = ConfigureConstant.ZHKC_PROPERTIES_PREFIX
        + ".swagger", name = "enabled", havingValue = "true", matchIfMissing = false)
@ConditionalOnClass({OpenAPI.class, GroupedOpenApi.class})
@Order(-2147483646)
public class SwaggerAutoConfiguration {
    //Knife4j官网地址 https://doc.xiaominfo.com/docs/quick-start
    //springdoc官网地址  https://springdoc.org/#demos

    @Value("${zhkc.name:}")
    private String appName;

    @Value("${zhkc.swagger.scan:}")
    private String getPackagesToScan;

    @PostConstruct
    public void init() {
        log.info("init SwaggerAutoConfiguration...");
    }


    /**
     * @Description
     * @Author zzw
     * @Date 13:21 2023/11/4
     **/
    @Bean
    public OpenAPI springShopOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title(appName)
                        .contact(new Contact().name("-").email("-").url("-"))
                        .description(appName)
                        .version("v1.0.0")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("SpringShop Wiki Documentation")
                        .url("https://springshop.wiki.github.org/docs"));
    }

    /**
     * @Description
     * @Author zzw
     * @Date 13:23 2023/11/4
     **/
    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("default")
                .pathsToMatch("/**")
                .packagesToScan(getPackagesToScan)
                .build();
    }
}
